var navBar = false

$(function () {
    initSwiper()
    initMultiTab()
    initNewsSwiper()
})

// Swiper
var swiperList = []
var swiperLength = 0
var timer = -1
var currentSwiperItem = 0

function initSwiper() {
    swiperList = $('#focus-box .focus-item')
    swiperLength = swiperList.length
    changeSwiperItem(0)
    setSwiperTimer()
    $('#prev_swiper').click(function () {
        prevSwiperItem()
    })
    $('#next_swiper').click(function () {
        nextSwiperItem()
    })
    resizeSwiper()
    $(window).resize(function () {
        resizeSwiper()
    })
}

function setSwiperTimer() {
    if (timer === -1) {
        timer = setInterval(() => {
            nextSwiperItem()
        }, 5000)
    }
}

function nextSwiperItem() {
    if (currentSwiperItem + 1 >= swiperLength)
        changeSwiperItem(0)
    else
        changeSwiperItem(currentSwiperItem + 1)
}

function prevSwiperItem() {
    if (currentSwiperItem - 1 <= 0)
        changeSwiperItem(swiperLength - 1)
    else
        changeSwiperItem(currentSwiperItem - 1)
}

function changeSwiperItem(index) {
    $(swiperList[currentSwiperItem]).removeClass('active')
    $(swiperList[index]).addClass('active')
    currentSwiperItem = index
}

// Resize Swiper
function resizeSwiper() {
    var box = $('#focus')
    var img = $('.focus-item img')
    var displyWidth = window.innerWidth
    var h = 3 * displyWidth / 10
    box.css('height', `${h}px`)
    img.css('height', `${h}px`)
}

// MultiTab
var multiTabDisplay = []
var multiTabElement = []
var multiTabContentElement = []
function initMultiTab() {
    var allBox = $('.news_box_item')
    // Init Element
    for (i = 0;i < allBox.length;i++) {
        var newsBox = allBox[i]
        multiTabElement.push($(newsBox).find('.title_block'))
        multiTabContentElement.push($(newsBox).find('.content_block'))
        multiTabDisplay.push(0)
    }
    // Init Current
    for (i = 0;i < multiTabDisplay.length;i ++) {
        changeMultiTab(i, 0)
    }
    // Mouse Move Event
    $('.title_block').mouseenter(event => {
        var dataset = event.currentTarget.dataset;
        var module = parseInt(dataset.module);
        var index = parseInt(dataset.index);
        changeMultiTab(module, index)
    })
}

function changeMultiTab(module, index) {
    var currentTitle = multiTabElement[module][multiTabDisplay[module]]
    var currentContent = multiTabContentElement[module][multiTabDisplay[module]]
    $(currentContent).removeClass('active')
    $(currentTitle).removeClass('active')
    var targetTitle = multiTabElement[module][index]
    var targetContent = multiTabContentElement[module][index]
    $(targetTitle).addClass('active')
    $(targetContent).addClass('active')
    multiTabDisplay[module] = index
}

// News Swiper
var newsSwiperList = []
var newsSwiperDot = []
var currentNewsSwiperItem = 0
var newsSwiperTimer = -1
function initNewsSwiper() {
    newsSwiperList = $('.news_swiper_item')
    initNewsSwiperDot()
    newsSwiperDot = $('.news_swiper_dot')
    switchNewsSwiper(currentNewsSwiperItem)
    createTimer()
    $('.news_swiper_dot').click(event => {
        var target = event.currentTarget.dataset.index
        switchNewsSwiper(parseInt(target))
    })
    resizeNewsSwiper()
    $(window).resize(() => {
        resizeNewsSwiper()
    })
}

function resizeNewsSwiper() {
    var boxWidth = $('.news_swiper')[0].offsetWidth;
    var height = boxWidth * 3 / 4
    $('.news_swiper_img').css('height', `${height}px`)
    $('.news_swiper').css('height', `${height}px`)
}

function createTimer() {
    newsSwiperTimer = setInterval(() => {
        newsSwiperNext()
    }, 5000)
}

function newsSwiperNext() {
    if (currentNewsSwiperItem + 1 >= newsSwiperList.length)
        switchNewsSwiper(0)
    else
        switchNewsSwiper(currentNewsSwiperItem + 1)
}

function initNewsSwiperDot() {
    for (i = 0;i < newsSwiperList.length;i++) {
        var template = `<li class="news_swiper_dot" data-index="${i}">${i + 1}</li>`
        $('.news_swiper_dot_box').append($(template))
    }
}

function switchNewsSwiper(index) {
    $(newsSwiperDot[currentNewsSwiperItem]).removeClass('active')
    $(newsSwiperList[currentNewsSwiperItem]).removeClass('active')
    $(newsSwiperList[index]).addClass('active')
    $(newsSwiperDot[index]).addClass('active')
    currentNewsSwiperItem = index
}